<!-- 计划 -->
<template>
	<el-drawer v-model="isShowDrawer" direction="rtl" :before-close="handleClose" :size="curDrawerSize" :close-on-click-modal="isReadOnly">
		<template #header="{ titleId, titleClass }">
			<div class="zs-drawer-header" :id="titleId" :class="titleClass">
				<i class="icon-b-item zs-icon zs-text-blue zs-space-right-sm"></i>
				<div class="zs-title zs-text-black">报批</div>
			</div>
		</template>

		<template #default>
			<el-container class="zs-drawer-container">
				<el-scrollbar class="zs-drawer-content">
					<div class="zs-card zs-space-bottom">
						<HsForm :filed-list="trialFiledList" ref="infoForm" :isDisabled="isReadOnly"></HsForm>
					</div>
				</el-scrollbar>
			</el-container>
		</template>

		<template #footer>
			<div>
				<el-button class="zs-btn-md zs-btn-primary" @click="doConfirm(1)">保存</el-button>
				<el-button class="zs-btn-md zs-btn-primary" @click="doConfirm(2)">提交</el-button>
			</div>
		</template>
	</el-drawer>
</template>

<script setup>
	import HsForm from '@/components/hsForm.vue';
	import { trialFiledList } from '@/config/form/settlement.js';
	import { ref, watch, nextTick } from 'vue';
	import { useStore } from 'vuex';
	import { ElMessage } from 'element-plus';
	import { deepClone } from '@/utils/publicUtils';
	import { saveOrAuditReportApi, saveOrAuditReportInfoApi } from '@/api/SpecialReport/settlementReport';

	const props = defineProps({
		isShow: { type: Boolean, default: false },
		isReadOnly: { type: Boolean, default: true },
		handleType: { type: String, default: '' },
		currentRow: { type: Object },
	});

	let store = useStore();
	const curDrawerSize = ref(store.state.uiDrawerSize);

	const emit = defineEmits(['close']);

	let isShowDrawer = ref(props.isShow);

	const infoForm = ref();

	watch(
		() => props.isShow,
		newVal => {
			isShowDrawer.value = newVal;
			nextTick(() => {
				newVal && getInfo();
			});
		},
	);

	//提交
	const doConfirm = async typeId => {
		let model = await infoForm.value.validateForm();
		if (!model) return;
		model.id = props.currentRow.id;
		model.auditStatus = typeId;
		if (model.bpmAttachType == 1) {
			model.reportUploadOss = null;
		} else {
			model.reportUploadOss = model.reportUploadOssList.length > 0 ? model.reportUploadOssList[0] : null;
		}
		let res = await saveOrAuditReportApi(model);
		if (res.data.code == 200) {
			ElMessage({ message: '提交成功', type: 'success' });
			isShowDrawer.value = false;
			emit('close', { isShow: false, isRefresh: true });
		}
	};

	//详情
	const getInfo = async () => {
		let res = await saveOrAuditReportInfoApi(props.currentRow.id);
		let resData = deepClone(res.data.data);
		resData.reportUploadOss.name = resData.reportUploadOss.originalName;
		resData.bpmOtherAttachList.forEach(item => (item.name = item.originalName));
		resData.bpmOtherAttachList = resData.bpmOtherAttachList || [];
		resData.reportUploadOssList = resData.reportUploadOss ? [resData.reportUploadOss] : [];
		infoForm.value.setFiledVal(resData);
	};

	//关闭抽屉
	const handleClose = done => {
		emit('close', { isShow: false });
		done();
	};
</script>
